package com.xinghou.platform.server.api.bussiness.impl;

import com.xinghou.platform.dao.bases.util.StringUtils;
import com.xinghou.platform.dao.main.StoreDAO;
import com.xinghou.platform.entity.req.ListReq;
import com.xinghou.platform.model.main.Store;
import com.xinghou.platform.model.main.StoreStatistics;
import com.xinghou.platform.model.main.WaitStore;
import com.xinghou.platform.server.api.bussiness.StoreService;
import com.xinghou.platform.server.mq.MqMessage;
import com.xinghou.platform.util.map.GeoHashUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * Created by liufangyu on 2016/6/24.
 */
@Service("ShopService")
public class StoreServiceImpl implements StoreService {
    @Resource
    private StoreDAO auditDao;
    @Resource
    private MqMessage mqMessage;



    @Override
    public List<Store> list(Map<String,Object> map) {
        if(!map.containsKey("start")){
            map.put("start",0);
        }else{
            map.put("start",Integer.parseInt(map.get("start").toString()));
        }
        if(!map.containsKey("limit")){
            map.put("limit",20);
        }else{
            map.put("limit",Integer.parseInt(map.get("limit").toString()));
        }
        List<Store> list = auditDao.list(map);
        for(Store shop_info:list) {
            String thumurl = shop_info.getThumurl();
            String photos = shop_info.getPhotos();
            if(StringUtils.isNullOrEmpty(thumurl)){
                shop_info.setThumurl_count(0);
            }else {
                shop_info.setThumurl_count(thumurl.split(",").length);
            }
            if(StringUtils.isNullOrEmpty(photos)) {
                shop_info.setPhotos_count(0);
            }else {
                shop_info.setPhotos_count(photos.split(",").length);
            }
        }
        return list;
    }

    @Override
    public  Store get(int id) {
        return auditDao.get(id);
    }

    @Override
    public  int save(Map<String,Object> map) {
        return auditDao.save(map);
    }

    @Override
    public void addWaitStore(WaitStore waitStore) {
        waitStore.createtime=System.currentTimeMillis();
        auditDao.addWaitStore(waitStore);

    }

    @Override
    public void addShareCountStatstics(Integer storeid) {
        auditDao.addShareCountStatstics(storeid);
        mqMessage.sendMessageStore(storeid.toString(),true);

    }

    @Override
    public void reduceShareCountStatstics(Integer storeid) {
        auditDao.reduceShareCountStatstics(storeid);
        mqMessage.sendMessageStore(storeid.toString(),true);

    }
    @Override
    public List<Store> storeList(ListReq listReq){
        return auditDao.storeList(listReq);
    }

    @Override
    public void updateStore(Store store){
        auditDao.updateStore(store);
        mqMessage.sendMessageStore(store.getId().toString(),true);
    }

    @Override
    public void delStore(Store store){
        auditDao.delStore(store);
    }

    @Override
    public void addStore(Store store){
        Long timstamp =System.currentTimeMillis();
        store.setCreatetime(String.valueOf(timstamp));
        store.setGeohash(GeoHashUtil.encode(store.getLatitude(),store.getLongitude()));
        auditDao.addStore(store);
        StoreStatistics storeStatistics = new StoreStatistics();
        storeStatistics.setSharecount(0);
        storeStatistics.setStoreid(store.getId());
        storeStatistics.setCreatetime(timstamp);
        storeStatistics.setUpdatetime(timstamp);
        auditDao.addStoreStatistics(storeStatistics);
        mqMessage.sendMessageStore(store.getId().toString(),false);
    }
}